﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KYO.DAL;
using System.Data;
using System.Security;
using ObjectPersistence;

public partial class Admin_Controls_Mod_ModuleManager_ModuleManagerSetup : Controller<Module>
{
    List<Message> list = new List<Message>();
    ModuleAssembler moduleService = new ModuleAssembler();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            MessageBox1.Visible = false;
            Model = new Module();
            Model.IsNew = true;

        }
    }

    protected override void OnInit(EventArgs e)
    {
       
        InitializeComponent();
        base.OnInit(e);
    }

    private void InitializeComponent()
    {
        SaveTool1.SaveClick += new EventHandler(SaveTool1_SaveClick);
        SaveTool1.CancelClick += new EventHandler(SaveTool1_CancelClick);
        SaveTool1.ApplyClick += new EventHandler(SaveTool1_ApplyClick);
    }

    void SaveTool1_ApplyClick(object sender, EventArgs e)
    {

        try
        {

            UpdateModel();
            moduleService.MakePersistent(Model,true);
            list.Add(new Message("Apply Is successful!"));
            ViewMessage(list, MessageType.Success);

        }
        catch (Exception ex)
        {
            list.Add(new Message("Update Is Fail!"));
            list.Add(new Message(ex.Message));
            ViewMessage(list, MessageType.Error);
        }
    }

    private void UpdateModel()
    {
 	     DataSet ds = ReadConfig();

        DataTable table = ds.Tables["module"];
        DataRow row = table.Rows[0];
        //Response.Write(row["modulePath"]);
        //Response.Write(row["fileName_Text"]);

       
        Model.Author = row["author"].ToString();
        Model.CopyRight = row["copyRight"].ToString();
        Model.Description = row["description"].ToString();
        Model.Name = row["name"].ToString();
        Model.Version = row["version"].ToString();

       table = ds.Tables["filename"];
       row = table.Rows[0];
       Model.ModulePath = row["modulePath"].ToString();
       Model.FileName = row["filename_Text"].ToString();
    }

    void SaveTool1_CancelClick(object sender, EventArgs e)
    {
        View(PageView.ModuleManager, Action.LIST);
    }

    void SaveTool1_SaveClick(object sender, EventArgs e)
    {

        try
        {
            UpdateModel();
            Model.IsNew = true;
            moduleService.MakePersistent(Model,true);
            View(PageView.ModuleManager, Action.LIST);
       
        }
        catch (Exception ex)
        {


            list.Add(new Message("Update Is fail!"));
            list.Add(new Message(ex.Message));
            ViewMessage(list, MessageType.Error);
        }

    }
    #region Config Reader
    public string ModuleConfigFile
    {
        get
        {

            return Server.MapPath("~/Modules/") +tb_ModulePath.Text+ @"\ModuleConfig.config";
        }
    }
    public DataSet ReadConfig()
    {
        DataSet ds = null;
        try
        {
            if (System.IO.File.Exists(ModuleConfigFile))
            {
                ds = new DataSet();

                ds.ReadXml(ModuleConfigFile);
            }
        }
        catch (SecurityException e)
        {
            // Do not throw exceptions

            return null;
        }


        return ds;
    }
    #endregion

}
